package com.app.Activity;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import app.com.ModuleRole.R;

import com.app.Adapter.RoleAdapter;
import com.app.Adapter.RuleAdapter;
import com.app.Controller.ReadWriteFileController;
import com.app.DTO.RoleDTO;
import com.app.PhanQuyen.Quyen;
import com.app.RowDTO.RoleRowDTO;
import com.app.RowDTO.RuleRowDTO;

public class frmDanhSachVaiTroMacDinh extends Activity {
	ListView lvDanhSachVaiTro;
	public ArrayList<RoleRowDTO> data;
	
	Button btnHoanTat;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		
		setContentView(R.layout.layout_vai_tro_mac_dinh);
		lvDanhSachVaiTro = (ListView) findViewById(R.id.lvDanhSachVaiTro);
		btnHoanTat=(Button)findViewById(R.id.btnHoanTat);
		lvDanhSachVaiTro.setChoiceMode(2);
		lvDanhSachVaiTro.setTextFilterEnabled(true);
		fillListView();		
		
		btnHoanTat.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {			
				ArrayList<RoleDTO> roleList=getSelectedRole();
				String s="";
				for(RoleDTO role:roleList){
					ReadWriteFileController.insertRole(ReadWriteFileController.ROLE_SETTING_PATH, role);
					s+=role.getName()+"\t";
				}
				Toast.makeText(getBaseContext(), s, 1).show();
				finish();
			}
		});
	}
	void fillListView() {
		ArrayList<RoleDTO> roleList=ReadWriteFileController.readRole(ReadWriteFileController.ROLE_DEFAULT_PATH);
		
		 data=new ArrayList<RoleRowDTO>();
			for(RoleDTO role:roleList){
			
				RoleRowDTO row=new RoleRowDTO();
				row.role=role;
				row.checked=false;
				data.add(row);
			}
			RoleAdapter adapter=new RoleAdapter(this, data);
			lvDanhSachVaiTro.setAdapter(adapter);
			

	}
	private ArrayList<RoleDTO> getSelectedRole(){
		ArrayList<RoleDTO> roleList=new ArrayList<RoleDTO>();
		int n=data.size();
		for(int i=0;i<n;i++){
			if(data.get(i).checked){
				roleList.add(data.get(i).role);
			}
		}
		return roleList;
	}
}
